package main

import (
	"bianwang/Oss/handler"
	pb "bianwang/Oss/proto"
	"bianwang/Oss/subscriber"
	"bianwang/web/utils"
	"context"
	"github.com/asim/go-micro/v3"
	"github.com/asim/go-micro/v3/registry"
	"github.com/topwhere/go-micro/registry/nacos"
)

const (
	ServerName = "go.micro.srv.Oss" // server name
)

func main() {
	reg := nacos.NewRegistry(func(options *registry.Options) {
		if options.Context == nil {
			options.Context = context.Background()
		}
		options.Context = context.WithValue(options.Context, nacos.AddressKey{}, utils.GetAddrs())
		options.Context = context.WithValue(options.Context, nacos.ConfigKey{}, utils.GetNacosClientConfig())
	})
	// Create service
	service := micro.NewService(
		micro.Name(ServerName),
		micro.Version("latest"),
		micro.Registry(reg),
	)

	// Initialise service
	service.Init()

	// Register handler
	if err := pb.RegisterOssHandler(service.Server(), new(handler.Oss)); err != nil {
		utils.AlyLog("Register handler 失败", err.Error())
	}

	// Register Struct as Subscriber
	if err := micro.RegisterSubscriber(ServerName, service.Server(), new(subscriber.Oss)); err != nil {
		utils.AlyLog("egister Struct as Subscriber 失败", err.Error())
	}

	// Register Function as Subscriber
	if err := micro.RegisterSubscriber(ServerName, service.Server(), subscriber.Handler); err != nil {
		utils.AlyLog("Register Function as Subscriber 失败", err.Error())
	}

	// Run service
	if err := service.Run(); err != nil {
		utils.AlyLog("Run service 失败", err.Error())
	}
}
